<template>
   <div class="wrapper">
      <div class="topbar" :data="categoryL2List" >
         <!-- 顶部导航 -->
         <div class="topCenter">
            <router-link to="/" href=" " class="home">
               <van-icon name="wap-home-o" size="34" color="#333" />
            </router-link>
            <span class="logo">网易严选</span>
            <router-link to="/search" href="" class="search">
               <van-icon name="search" size="34" color="#333" />
            </router-link>
            <router-link to="/cart" href="" class="shopCar">
               <van-icon name="shopping-cart-o" size="34" color="#333" />
            </router-link>
         </div>
         <!-- 导航 -->
         <div class="topNav">
            <van-tabs @click-tab="handlerCategory" v-model:active="active" title-active-color="#dd1a21" line-height=".03rem" color="#dd1a21">
               <van-tab :name="categoryL2.id" v-for="categoryL2 in categoryL2List" :title="categoryL2.name"></van-tab>
            </van-tabs>
         </div>
         <van-divider />
         <!-- 内容 -->
         <div class="content" :data="categoryItems">
            <span class="title">{{ categoryItems.category?.frontName}}</span>
            <div class="contentItemcontainer">
               <div class="contentItem" v-for="item in categoryItems.itemList" :key="item.id" @click="$router.push('/detail')">
                  <img :src="item.listPicUrl">
                  <span class="contentItemspan">{{item.name}}</span>
                  <div class="redpacket" v-if="item.itemTagList[0]?.type">
                     <van-icon name="point-gift" color="red" />
                     {{ item.itemTagList[0]?.name }}
                  </div>
                  <span class="atlast" v-if="item.finalPriceInfoVO.priceInfo.finalPrice">
                     {{ item.finalPriceInfoVO.priceInfo.finalPrice.prefix }}￥</span><span class="price">
                     {{ item.retailPrice }}
                  </span>
                  <div class="activity" v-if="item.finalPriceInfoVO.banner">{{ item.finalPriceInfoVO.banner.content }}</div>
               </div>
            </div>
         </div>
      </div>
   </div>
</template>


<script setup lang='ts'>
import { ref,onMounted } from 'vue';
import { useRoute } from 'vue-router';
import itemApi from '@/api/item'

const active = ref(0);
//商品列表请求获取数据
const route = useRoute()
const subCategoryId = route.query.subCategoryId;
const categoryId = route.query.categoryId
const categoryL2List = ref<any>([])
const categoryItems = ref<any>([])
const getGoodsList = async(subCategoryId: unknown,categoryId: unknown) => {
   try {
    let result = await itemApi.getGoodsList(subCategoryId as unknown as number,categoryId as unknown as number)
    //console.log(result);
    categoryL2List.value = result.data.categoryL2List
    categoryItems.value = result.data.categoryItems
  } catch (error) {
    //console.log(error);
    alert('获取商品数据失败！')
  }
}
//页面挂载
onMounted(() => {
   getGoodsList(subCategoryId,categoryId);
})
//点击商品类标导航栏切换内容区
const handlerCategory = (tid:any,title:any) => {
   const subCategoryId2 = tid.name
   //console.log(subCategoryId2);
   getGoodsList(subCategoryId2,categoryId)
}
</script>

<style scoped>
.wrapper .topbar {
   width: 10rem;
   height: 1.17rem;
   background-color: #fafafa;
   position: relative;
}

.wrapper .topCenter {
   width: 10rem;
   height: 1.17rem;
   line-height: 1.17rem;
   border-bottom: .03rem solid #f0f0fb;
   position: fixed;
   top: 0;
   left: 0;
   z-index: 1;
   background-color: white;
}

.wrapper.topbar.home {
   margin-left: .27rem;

}

.wrapper .logo {
   font-size: .53rem;
   color: #333;
   margin-left: 2.67rem;
   margin-right: 1.47rem;
   font-weight: 700;
   line-height: 1.47rem;
}

.wrapper .search {
   margin-left: .4rem;

}

.wrapper .shopCar {
   margin-left: .4rem;
}

.topNav {
   position: fixed;
   top: 1.17rem;
   left: 0;
   height: 1.07rem;
   background-color: #dfdfdf;
}

::v-deep(.van-tabs__nav) {
   height: .8rem;
}

.content {
   width: 10rem;
   margin-top: 2.13rem;
}

.content .title {
   width: 100%;
   display: block;
   padding-left: 3.2rem;
   font-size: .37rem;
   margin: .27rem 0;
}

.content .contentItem {
   width: 4.6rem;
   height: 8.07rem;
   margin: .19rem;
}

.contentItem img {
   width: 4.6rem;
   height: 4.6rem;
   border-radius: .27rem;
}

.contentItemspan {
   font-size: .32rem;
   display: block;
}

.redpacket {
   font-size: .32rem;
   border-radius: .27rem;
   border: .03rem solid #fafafa;
   background-color: #f0f0fb;
   width: 1.87rem;
}

.contentItemcontainer {
   width: 10rem;
   display: flex;
   /* 子元素溢出换行 */
   flex-flow: row wrap
}

.atlast {
    color: red;
    font-size: .32rem;
}

.price {
   color: red;
   font-size: .53rem;
   font-weight: 700;
}

.activity {
   font-size: .32rem;
   border-radius: .27rem;
   border: .03rem solid #fafafa;
   background-color: red;
   color: #f0f0fb;
   text-align: center;
   margin-top: .13rem;
   width: 2.13rem;
}
</style>